﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Concurrency;
using System.Collections.Generic;
using Caliburn.Micro;
using PicturePortfolio.ServiceReference;
namespace PicturePortfolio.Silverlight.ViewModels
{
    public class CategoryListViewModel:PropertyChangedBase
    {
        PhotoServiceClient _photoService;
        public CategoryListViewModel(PhotoServiceClient service)
        {
            _photoService = service;
            this.LoadCategories();
        }
        private void LoadCategories()
        {
            var func = Observable.FromEvent<GetCategoriesCompletedEventArgs>(_photoService, "GetCategoriesCompleted")
                .ObserveOn(Scheduler.ThreadPool);
            _photoService.GetCategoriesAsync();

            func.ObserveOn(Scheduler.Dispatcher)
                .Select(result => result.EventArgs.Result)
                .Subscribe(s => this.Categories = s.ToList());
        }
        private List<Category> _categories;
        public List<Category> Categories
        {
            get { return _categories; }
            set
            {
                _categories = value;
                NotifyOfPropertyChange(() => Categories);
            }
        }

        public void CategorySelectionChanged(object item)
        {
            Message.EventAggregationProvider.EventAggregator.Publish<Message.RefreshPhotoPreviewMessage>(new Message.RefreshPhotoPreviewMessage { SelectedCategory=item as Category,Sender=this });
        }
    }
}
